<template>
  <el-form
    ref="customForm"
    :model="formData"
    :rules="formRules"
    label-width="110px"
  >
    <el-form-item v-if="action === 'edit'" prop="id">
      <input v-model="formData.id" type="hidden">
    </el-form-item>
    <el-form-item label="事件类型" prop="eventType">
      <el-select v-model="formData.eventType" :disabled="action === 'edit'" class="filter-item" placeholder="请选择事件类型">
        <el-option v-for="item in eventOptions" :key="item" :label="item | eventFilter" :value="item" />
      </el-select>
    </el-form-item>
    <el-form-item label="消息类型" prop="type">
      <el-select v-model="formData.type" class="filter-item" placeholder="请选择消息类型">
        <el-option v-for="item in typeOptions" :key="item" :label="item | typeFilter" :value="item" />
      </el-select>
    </el-form-item>
    <el-form-item label="消息Id" prop="msgId">
      <el-input v-model="formData.msgId" type="text" placeholder="请输入公众号消息Id" />
    </el-form-item>
    <el-form-item v-show="formData.type==1 || formData.type>1" label="跳转url" prop="url">
      <el-input v-model="formData.url" type="text" placeholder="图文信息跳转地址" />
    </el-form-item>
    <el-form-item v-show="formData.type==1 || formData.type>1" label="图片url" prop="picUrl">
      <el-input v-model="formData.picUrl" type="text" placeholder="图文信息照片地址" />
    </el-form-item>
    <el-form-item v-show="formData.type==1 || formData.type>1" label="标题" prop="title">
      <el-input v-model="formData.title" type="text" placeholder="图文信息标题" />
    </el-form-item>
    <el-form-item v-show="formData.type==1 || formData.type>1" label="描述" prop="desc">
      <el-input v-model="formData.desc" type="textarea" placeholder="图文信息描述" :autosize="{ minRows: 3 }" />
    </el-form-item>
    <el-form-item v-show="formData.type==0 || formData.type>1" label="消息内容" prop="content">
      <el-input v-model="formData.content" type="textarea" placeholder="请输入公众号消息内容" :autosize="{ minRows: 3 }" />
    </el-form-item>
    <el-form-item label="简介" prop="msgDesc">
      <el-input v-model="formData.msgDesc" type="textarea" placeholder="请输入公众号消息简介" :autosize="{ minRows: 3 }" />
    </el-form-item>
  </el-form>
</template>
<script>
import { get, save, update } from '@/api/wx/mp/mp-msg';

export default {
  name: 'WxMpConfigForm',
  components: {
  },
  filters: {
    typeFilter(type) {
      const typeMap = {
        0: '文本消息',
        1: '图文消息',
        2: '图文+文本消息'
      };
      return typeMap[type];
    },
    eventFilter(eventType) {
      const eventMap = {
        1: '关注公众号',
        2: '扫描事件',
        3: '菜单跳转',
        4: '点击菜单',
        5: '客服消息开始',
        6: '客服消息结束'
      };
      return eventMap[eventType];
    }
  },
  props: {
    action: {
      type: String,
      default: 'add'
    },
    appId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      typeOptions: [0, 1, 2],
      eventOptions: [1, 2, 3, 4, 5, 6],
      formRules: {
        // appId: [
        //   { required: true, message: 'AppId不能为空！', trigger: 'blur' }
        // ]
      },
      formData: {
        id: null,
        appId: this.appId,
        eventType: null,
        type: null,
        msgId: null,
        url: null,
        picUrl: null,
        title: null,
        desc: null,
        content: null,
        msgDesc: null
      }
    };
  },
  methods: {
    getInfo(id) {
      get(id).then(response => {
        const result = response.data;
        if (result.code === 0) {
          this.formData = result.data;
        } else {
          this.$notify.error({
            title: result.code,
            message: result.msg
          });
        }
      });
    },
    handleSubmit() {
      this.$refs.customForm.validate((valid) => {
        if (valid) {
          if (this.action === 'add') {
            save(this.formData).then(response => {
              this.handleResult(response.data);
            });
          } else if (this.action === 'edit') {
            update(this.formData).then(response => {
              this.handleResult(response.data);
            });
          }
        }
      });
    },
    handleResult(obj) {
      if (obj.code === 0) {
        this.$notify({
          title: '提示',
          message: obj.msg,
          type: 'success'
        });
        this.$parent.$parent.$refs.customTable.query();
        this.reset();
        this.$parent.$parent.formInitOption.showModal = false;
      } else {
        this.$notify.error({
          title: obj.code,
          desc: obj.msg
        });
      }
    },
    reset() {
      this.$refs.customForm.resetFields();
    },
    encodeUrl() {
      this.formData.redirectUri = encodeURIComponent(this.formData.redirectUri);
    },
    decodeUrl() {
      this.formData.redirectUri = decodeURIComponent(this.formData.redirectUri);
    }
  }
};
</script>
